using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimeSheet.Protected
{
    /// <summary>
    /// Summary description for ProjectSummaryReport.
    /// </summary>
    public class ProjectSummaryReport : Page
    {
        protected Label lblContent;
        protected Label lblDateRange;

        private void Page_Load(object sender, EventArgs e)
        {
            RenderProjectSummaries(GetDateFrom(), GetDateTo());
            lblDateRange.Text = "From "
                                + GetDateFrom().ToShortDateString() + " to "
                                + GetDateTo().ToShortDateString();
        }

        private DateTime GetDateFrom()
        {
            return Convert.ToDateTime(Server.UrlDecode(Request["from"]));
        }

        private DateTime GetDateTo()
        {
            return Convert.ToDateTime(Server.UrlDecode(Request["to"]));
        }

        private void RenderProjectSummaries(DateTime from, DateTime to)
        {
            string projName = "";
            DataTable dt =
                TimeSheetBLL.Project.GetProjectHoursForSummaryByDate(
                    GetDateFrom(), GetDateTo());
            foreach (DataRow row in dt.Rows)
            {
                string user = row["first_name"] + " " + row["last_name"];
                string project = row["client_name"] + " - " + row["project_name"];
                string hours = row["total_hours"].ToString();

                if (project == projName)
                {
                    lblContent.Text += "&nbsp;&nbsp;" + user + ": " + hours +
                                       " hours.<br />";
                }
                else
                {
                    lblContent.Text += "<h3>" + project + "</h3>";
                    lblContent.Text += "&nbsp;&nbsp;" + user + ": " + hours +
                                       " hours.<br />";
                }
                projName = project;
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}